package edu.gxmzu.task.utils;

import lombok.Builder;
import lombok.Data;

/**
 * 结果类，响应回前端的格式
 */
@Data
@Builder
public class Result<T> {

    private Integer code;
    private String msg;
    private T data;

    private static final Integer SUCCESS = 200;  // 成功
    private static final Integer BAD_REQUEST = 400;  // 无法处理，缺少参数等
    private static final Integer UNAUTHORIZED = 401;  // 认证失败
    private static final Integer FORBIDDEN = 403;  // 权限不足
    private static final Integer NOT_FOUND = 404;  // 找不到
    private static final Integer METHOD_NOT_ALLOWED = 405;  // 请求方法不允许
    private static final Integer CONFLICT = 409;  // 冲突
    private static final Integer FAIL = 500;  // 失败

    public static Result ok(String msg) {
        return Result.builder().code(SUCCESS).msg(msg).data(null).build();
    }
    public static <T> Result ok(String msg, T data) {
        return Result.builder().code(SUCCESS).msg(msg).data(data).build();
    }

    /**
     * 无法处理，缺少参数等
     * @param msg 响应信息
     */
    public static Result badRequest(String msg) {
        return Result.builder().code(BAD_REQUEST).msg(msg).data(null).build();
    }
    /**
     * 认证失败
     * @param msg 响应信息
     */
    public static Result unauthorized(String msg) {return Result.builder().code(UNAUTHORIZED).msg(msg).data(null).build();}
    /**
     * 权限不足
     * @param msg 响应信息
     */
    public static Result forbidden(String msg) {return Result.builder().code(FORBIDDEN).msg(msg).data(null).build();}
    /**
     * 冲突
     * @param msg 响应信息
     */
    public static Result conflict(String msg) {return Result.builder().code(CONFLICT).msg(msg).data(null).build();}
    public static Result methodNotAllowed(String msg) {return Result.builder().code(METHOD_NOT_ALLOWED).msg(msg).data(null).build();}
    public static Result notFound(String msg) {return Result.builder().code(NOT_FOUND).msg(msg).data(null).build();}
    public static Result fail(String msg) {
        return Result.builder().code(FAIL).msg(msg).data(null).build();
    }




}

